package com.ego.coupon.service.impl;

import com.ego.coupon.po.MemberPrice;
import com.ego.coupon.service.MemberPriceService;
import com.ego.coupon.vo.SkuReductionVo;
import com.ego.coupon.vo.spu.MemberPriceVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.ego.coupon.dao.SkuFullReductionDao;
import com.ego.coupon.po.SkuFullReduction;
import com.ego.coupon.service.SkuFullReductionService;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @program: ego
 * @description 商品满减信息对应实现类
 * @author KamTang
 * @date 2022-11-13 13:17:12
 */
@Service("skuFullReductionService")
public class SkuFullReductionServiceImpl extends ServiceImpl<SkuFullReductionDao, SkuFullReduction> implements SkuFullReductionService {

    @Autowired
    private MemberPriceService memberPriceService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveSkuReduction(SkuReductionVo vo) {
        // 1、保存满减打折，会员价


        // 2、保存满减信息
        SkuFullReduction skuFullReduction = SkuFullReduction.fromVo(vo);
        if (skuFullReduction.getFullPrice().compareTo(BigDecimal.ZERO) > 0) {
            this.save(skuFullReduction);
        }

        // 3、会员价格
        List<MemberPriceVo> memberPriceVos = vo.getMemberPriceVo();
        List<MemberPrice> memberPrices = memberPriceVos.stream().map(price -> MemberPrice.build(vo, price))
                .filter(price -> price.getMemberPrice().compareTo(BigDecimal.ZERO) > 0).collect(Collectors.toList());
        memberPriceService.saveBatch(memberPrices);

    }

}
